//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <bsp.h>
#include <init.h>
#include <8254.h>

#define PIT_LATCH   \
    ((uint16_t)((PIT_ClockRate + krn.uTickRate / 2) / krn.uTickRate))

EXTERN_C DECL_CODEINIT void CDECL InitPIT()
{
    Outb(Control_Port,
        ControlWord_Mode2 + ControlWord_RWBytes + ControlWord_SelectCounter0);

    Outb(Counter0_Port, LOBYTE(PIT_LATCH));
    Outb(Counter0_Port, HIBYTE(PIT_LATCH));
}

EXTERN_C void CDECL BspReloadPIT()
{
}

EXTERN_C uint_t CDECL BspGetPITTime()
{
    Outb(Control_Port, ControlWord_SelectCounter0 + ControlWord_Latch);

    uint16_t u16Count = Inb(Counter0_Port);
    u16Count += Inb(Counter0_Port) << 8;

    return (PIT_LATCH - 1 - u16Count) * PIT_ClockCycle;
}
